// @ts-check
const path = require("path")
const minimist = require("minimist")

const args = minimist(process.argv.slice(2))
/** @type { "local" | "development" | "production" } */
const mode = args.mode || "development"
const baseUrlMap = new Map([
  ["local", "http://192.168.18.100:2201"],
  ["development", "https://csyikaoapi.91goodschool.com"],
  ["production", "https://yikaoapi.91goodschool.com"]
])

const config = {
  projectName: "xiaojiaApp",
  date: "2021-11-3",
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  alias: {
    "@/components": path.resolve(__dirname, "..", "src/components"),
    "@/utils": path.resolve(__dirname, "..", "src/utils"),
    "@/vendor": path.resolve(__dirname, "..", "src/vendor"),
    "@/store": path.resolve(__dirname, "..", "src/store"),
    "@/constants": path.resolve(__dirname, "..", "src/constants"),
    "@/apis": path.resolve(__dirname, "..", "src/apis"),
    "@/styles": path.resolve(__dirname, "..", "src/styles"),
    "@/images": path.resolve(__dirname, "..", "src/images"),
    "@/platform": path.resolve(__dirname, "..", "src/platform"),
    "@/types": path.resolve(__dirname, "..", "types")
  },
  /*
   * sass: {
   *   resource: [
   *     "src/styles/theme/style.scss"
   *   ],
   *   projectDirectory: path.resolve(__dirname, "..")
   * },
   */
  sourceRoot: "src",
  outputRoot: `dist/${process.env.TARO_ENV}`,
  plugins: [
    [
      path.resolve(__dirname, "..", "plugins/movePluginExport.js"),
      {
        /** @type {import("../plugins/movePluginExport.js").MovePluginExportOptions["inject"]} */
        inject: {
          ignores(root) {
            return ["pages/school"].includes(root)
          },
          injections: [
            {
              path: "config.js",
              data: {
                __BASE_URL__: baseUrlMap.get(mode)
              }
            }
          ]
        }
      }
    ],
    [
      path.resolve(__dirname, "..", "plugins/defineMicros.js"),
      {
        enableShareAppMessage: true
      }
    ],
    path.resolve(__dirname, "..", "plugins/promotion.js"),
    "taro-plugin-tailwind",
    [
      "@tarojs/plugin-inject",
      {
        components: {
          View: {
            hoverClass: "'hello'" // can be any string other then 'none'!
          },
          RichText: {
            userSelect: "true",
            selectable: "true",
            preview: "true",
            space: "'nbsp'"
          },
          Text: {
            selectable: "true",
            userSelect: "true",
            overflow: "'visible'",
            "max-lines": ""
          },
          Button: {
            bindchooseavatar: "eh"
          },
          Textarea: {
            "confirm-type": "'return'",
            "confirm-hold": "false",
            "adjust-keyboard-to": "'cursor'"
          },
          Video: {
            "show-bottom-progress": "true",
            "picture-in-picture-show-progress": "false",
            "picture-in-picture-init-position": "",
            "show-snapshot-button": "false",
            "show-background-playback-button": "false",
            "background-poster": "",
            "referrer-policy": "'no-referrer'",
            "is-drm": "false",
            "is-live": "false",
            "provision-url": "",
            "certificate-url": "",
            "license-url": "",
            "preferred-peak-bit-rate": "",
            bindcastinguserselect: "eh",
            bindcastingstatechange: "eh",
            bindcastinginterrupt: "eh"
          },
          ScrollView: {
            type: "'list'",
            "associative-container": "",
            reverse: "false",
            clip: "true",
            padding: "[0, 0, 0, 0]"
          },
          GridView: {
            type: "'aligned'",
            "cross-axis-count": "2",
            "max-cross-axis-extent": "0",
            "main-axis-gap": "0",
            "cross-axis-gap": "0",
            padding: "[0, 0, 0, 0]"
          },
          RootPortal: {
            enable: "true"
          },
          StickySection: {
            "push-pinned-header": "true",
            padding: "[0, 0, 0, 0]"
          },
          StickyHeader: {
            "offset-top": "0",
            padding: "[0, 0, 0, 0]"
          },
          Span: {},
          NestedScrollBody: {},
          NestedScrollHeader: {}
        },
        componentsMap: {
          GridView: "grid-view",
          RootPortal: "root-portal",
          StickySection: "sticky-section",
          StickyHeader: "sticky-header",
          Span: "span",
          NestedScrollBody: "nested-scroll-body",
          NestedScrollHeader: "nested-scroll-header"
        }
      }
    ]
  ],
  defineConstants: {
    __BASE_URL__: JSON.stringify(baseUrlMap.get(mode))
  },
  copy: {
    patterns: [
      { from: "src/sitemap.json", to: `dist/${process.env.TARO_ENV}/sitemap.json` },
      { from: "src/images", to: `dist/${process.env.TARO_ENV}/images` },
      { from: "src/swan-sitemap", to: `dist/swan/swan-sitemap` }
    ],
    options: {}
  },
  framework: "react",
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {}
      },
      url: {
        enable: true,
        config: {
          // 设定转换尺寸上限
          limit: 1024
        }
      },
      cssModules: {
        // 默认为 false，如需使用 css modules 功能，则设为 true
        enable: true,
        config: {
          // 转换模式，取值为 global/module
          namingPattern: "module",
          generateScopedName: "[name]__[local]___[hash:base64:5]"
        }
      }
    }
  },
  h5: {
    publicPath: "/",
    staticDirectory: "static",
    postcss: {
      autoprefixer: {
        enable: true,
        config: {}
      },
      cssModules: {
        // 默认为 false，如需使用 css modules 功能，则设为 true
        enable: true,
        config: {
          // 转换模式，取值为 global/module
          namingPattern: "module",
          generateScopedName: "[name]__[local]___[hash:base64:5]"
        }
      }
    }
  },
  env: {
    __MOCK_SRC__: JSON.stringify(
      "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
    ),
    __DEF_AVATAR__: JSON.stringify("/images/yikao-community/defAvatar.png"),
    __USER_AGREEMENT__: JSON.stringify("https://m.cnfzedu.com/user-agreement.html"),
    __PRIVACY_POLICY__: JSON.stringify("https://m.cnfzedu.com/privacy-policy.html"),
    __SERVICECHAT_URL__: JSON.stringify("https://work.weixin.qq.com/kfid/kfcc953e04ca428cf9c"),
    __SERVICECHAT_CORPID__: JSON.stringify("wwaef59f2aa3a6b263")
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === "development") {
    return merge({}, config, require("./dev"))
  }
  return merge({}, config, require("./prod"))
}
